package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductClientFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@RequestMapping("/api/item")
public class ItemApiController {

    @Autowired
    private ProductClientFeign productClientFeign;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    // 数据汇总
    @GetMapping("/{skuId}")
    public Map<String, Object> getItem(@PathVariable("skuId") Long skuId) {

        Map<String, Object> result = new HashMap<>();
        // 5部分数据

        CompletableFuture<SkuInfo> skuInfo1 = CompletableFuture.supplyAsync(() -> {
            // 1：商品详情
            SkuInfo skuInfo = productClientFeign.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> categoryView1 = skuInfo1.thenAcceptAsync(skuInfo -> {
            // 2：一、二、三级分类
            BaseCategoryView categoryView = productClientFeign.getCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", categoryView);
        }, threadPoolExecutor);

        CompletableFuture<Void> price1 = CompletableFuture.runAsync(() -> {
            // 3：商品价格
            BigDecimal price = productClientFeign.getPrice(skuId);
            result.put("price", price);
        }, threadPoolExecutor);

        CompletableFuture<Void> spuSaleAttrList = skuInfo1.thenAcceptAsync(skuInfo -> {
            // 4：销售属性 属性值 选中
            List<SpuSaleAttr> spuSaleAttrListCheckBySku =
                    productClientFeign.getSpuSaleAttrListCheckBySku(skuInfo.getId(), skuInfo.getSpuId());
            result.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
        }, threadPoolExecutor);

        CompletableFuture<Void> valuesSkuJson = skuInfo1.thenAcceptAsync(skuInfo -> {
            // 5：商品属性选择时 组合对应库存ID
            Map<String, String> skuValueIdsMap = productClientFeign.getSkuValueIdsMap(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSON.toJSONString(skuValueIdsMap));
        }, threadPoolExecutor);

        // 更新热度
        CompletableFuture.runAsync(() -> {
            listFeignClient.hotScore(skuId);
        }, threadPoolExecutor);

        // 等待所有线程
        CompletableFuture.allOf(skuInfo1, categoryView1, price1, spuSaleAttrList, valuesSkuJson).join();

        return result;
    }
}